(function() {
    var myApp = angular.module('myApp');

    myApp.run(function($rootScope, reqDataByAjax) {
    });

    myApp.controller("myMenuGroupTreeController", function($scope, $log, reqDataByAjax) {
        // 设置默认系统选项
        $scope.$root.selectedSystemId = 'DM';
        $scope.$root.$watch("selectedSystemId", function(newVal, oldVal, scope) {
            scope.$broadcast('refresh.tree');
        });

        // 设置菜单项树结构的Option
        function dataSource(parentFunction, callback) {
            reqDataByAjax("/scheduling/api/settings/menu/group", {
                system_id : $scope.$root.selectedSystemId,
                parent_id : parentFunction.group_id
            }).then(function(data) {
                var ret = [];
                for (var i = 0; i < data[0].length; ++i) {
                    var item = data[0][i];
                    item['type'] = 'folder';
                    ret.push(item);
                }
                callback({ data: ret });
            });
        }

        $scope.option = {
            dataSource: dataSource,
            multiSelect: false,
            folderSelect: true
        };

        // 记录当前选中组
        $scope.$on('selected.fu.tree', function(event, element, e, o) {
            if (element.is('#menuGroupTree')) {
                $scope.$root.selectedMenuGroup = o.selected[0];
                $scope.$root.$broadcast("show.tab", "#menuItemListTab");
            }
        });

        $scope.$on('deselected.fu.tree', function(event, element) {
            if (element.is('#menuGroupTree')) {
                $scope.$root.selectedMenuGroup = null;
                $scope.$root.$broadcast("show.tab", "#helpTab");
            }
        });
    });
    
    myApp.controller("addMenuGroupController", function($scope, $log
        , reqDataByAjax) {
        
        // TAB显示前初始化显示内容
        $scope.$on("show.bs.tab", function(event, element, target) {
            if (target.is("#addMenuGroupTab")) {
                $scope.menuGroup = {};
            }
        });

        // 调用服务新建菜单组
        var url = "/scheduling/api/settings/menu/addMenuGroup";
        $scope.ok = function() {
            var menuGroup = $scope.menuGroup;
            menuGroup.system_id = $scope.$root.selectedSystemId;
            menuGroup.parent_id = $scope.$root.selectedMenuGroup && $scope.$root.selectedMenuGroup.group_id;
            menuGroup.sort = +menuGroup.sort;
            reqDataByAjax(url, menuGroup, 'POST').then(function() {
                // 刷新树显示
                if (menuGroup.parent_id)
                    $scope.$root.$broadcast("refreshFolder.tree", "#menuGroupTree");
                else
                    $scope.$root.$broadcast("refresh.tree", "#menuGroupTree");
            });
        };
    });

    myApp.controller("modifyMenuGroupController", function($scope, $log
        , reqDataByAjax) {
        // TAB显示前初始化显示内容
        $scope.$on("show.bs.tab", function(event, element, target) {
            if (target.is("#modifyMenuGroupTab")) {
                $scope.menuGroup = $scope.$root.selectedMenuGroup;
            }
        });
        // 调用服务修改菜单组
        var url = "/scheduling/api/settings/menu/modifyMenuGroup";
        $scope.ok = function() {
            $scope.menuGroup.system_id = $scope.$root.selectedSystemId;
            $scope.menuGroup.sort = +$scope.menuGroup.sort;
            reqDataByAjax(url, $scope.menuGroup, "POST").then(function() {
                $scope.$root.$broadcast("updateItem.tree", "#menuGroupTree", $scope.menuGroup);
            });
        };
    });

    myApp.controller("deleteMenuGroupController", function($scope, $log
        , reqDataByAjax) {
        // 调用服务删除菜单组
        var url = "/scheduling/api/settings/menu/deleteMenuGroup";
        $scope.ok = function() {
            var menuGroup = $scope.$root.selectedMenuGroup;
            menuGroup.system_id = $scope.$root.selectedSystemId;
            reqDataByAjax(url, menuGroup, "POST").then(function() {
                if (menuGroup.parent_id)
                    $scope.$root.$broadcast("refreshParentFolder.tree", "#menuGroupTree");
                else
                    $scope.$root.$broadcast("refresh.tree", "#menuGroupTree");
            });
        };
    });

    myApp.controller("myMenuGroupTableController", function($scope, $log
        , myMenuGroupTableOption, reqDataByAjax) {
        // 设置组用户列表Option
        var url = "/scheduling/api/settings/menu/menu";
        $scope.option = myMenuGroupTableOption;
        $scope.option.ajax = ajax;
        function ajax(data, callback, settings) {
            data.group_id = $scope.$root.selectedMenuGroup && $scope.$root.selectedMenuGroup.group_id;
            reqDataByAjax(url, data).then(function (ret) {
                callback({
                    "draw": data.draw,
                    "recordsTotal": ret[1][0].recordsTotal,
                    "recordsFiltered": ret[2][0].recordsFiltered,
                    "data": ret[0]
                });
            });
        }
        // TAB显示时刷新菜单项列表
        $scope.$on("shown.bs.tab", function(event, element, target) {
            if (target.is("#menuItemListTab"))
                $scope.$root.$broadcast("refresh.table", "#menuItemtable");
        });
        // 记录菜单项选中情况
        $scope.$root.selectedMenu = [];
        $scope.$on("select", function(event, element, e, dt, type, indexes) {
            if (element.is("#menuItemtable"))
                $scope.$root.selectedMenu = Array.prototype.slice.call(dt.rows('.selected').data(), 0);
        });
        $scope.$on("deselect", function(event, element, e, dt, type, indexes) {
            if (element.is("#menuItemtable"))
                $scope.$root.selectedMenu = Array.prototype.slice.call(dt.rows('.selected').data(), 0);
        });
        $scope.$on("draw.dt", function(event, element, e, settings) {
            if (element.is("#menuItemtable"))
                $scope.$root.selectedMenu = [];
        });
    });

    myApp.controller("addMenuController", function($scope, $log, reqDataByAjax) {
        // TAB显示之前，设置待显示的菜单项信息
        $scope.$on("show.bs.tab", function(event, element, target) {
            if (target.is('#addMenuTab')) {
                $scope.menuGroup = {};
            }
        });

        // 保存输入的菜单项
        $scope.ok = function(e) {
            $scope.menuGroup.group_id = $scope.$root.selectedMenuGroup && $scope.$root.selectedMenuGroup.group_id;
            $scope.menuGroup.system_id = $scope.$root.selectedSystemId;
            $scope.menuGroup.sort = +$scope.menuGroup.sort;
            reqDataByAjax("/scheduling/api/settings/menu/addMenu", $scope.menuGroup, "POST").then(function(data) {
                // 保存后刷新菜单项列表
                $scope.$root.$broadcast("refresh.table", "#menuItemtable");
            });
        };
    });

    myApp.controller("modifyMenuController", function($scope, $log
        , reqDataByAjax) {
        // TAB显示前初始化显示内容
        $scope.$on("show.bs.tab", function(event, element, target) {
            if (target.is("#modifyMenuTab")) {
                $scope.menuGroup = $scope.$root.selectedMenu[0];
            }
        });
        // 调用服务修改菜单项
        var url = "/scheduling/api/settings/menu/modifyMenu";
        $scope.ok = function() {
            $scope.menuGroup.system_id = $scope.$root.selectedSystemId;
            $scope.menuGroup.sort = +$scope.menuGroup.sort;
            reqDataByAjax(url, $scope.menuGroup, "POST").then(function() {
                $scope.$root.$broadcast("refresh.table", "#menuItemtable", $scope.menuGroup);
            });
        };
    });

    myApp.controller("deleteMenuController", function($scope, $log, reqDataByAjax) {
        $scope.ok = function() {
            reqDataByAjax("/scheduling/api/settings/menu/deleteMenu", $scope.$root.selectedMenu, "POST").then(function() {
                // 保存后刷新菜单列表
                $scope.$root.$broadcast("refresh.table", "#menuItemtable");
            });
        };
    });

})();

